'use strict';
var cmsControllers = angular.module('cmsControllers');

cmsControllers
    .controller('CmsContactUsCtrl', ['$rootScope', '$scope', '$routeParams', 'StringUtil', 'uploadService', 'Subscription', 'ContactUsOpt', 'ContactUs',
        function ($rootScope, $scope, $routeParams, StringUtil, uploadService, Subscription, ContactUsOpt, ContactUs) {
            $scope.showContactUs = function() {
                $scope.contactUsContent = 'views/component/contactUs/contactUs-detail.html';
                var json = ContactUsOpt.opt({action: 'queryContactUsBySubId', subId: $rootScope.CurrentSub.id},function() {
                    $scope.contactUs = json.data.contactUs;
                });
            }
            $scope.showContactUs();

            $scope.editContactUs = function() {
                uploadService.maxNumb = 1;
                uploadService.files = [];
                $scope.contactUsContent = 'views/component/contactUs/contactUs-edit.html';

                if (!StringUtil.isEmpty($scope.contactUs.image)) {
                    uploadService.files.push({url: $scope.contactUs.image, thumbnailUrl: uploadService.imageUrl($scope.contactUs.image, 'thumbnail')});
                }
                $scope.files = uploadService.files;
            };
            $scope.saveContactUs = function() {
                $scope.contactUs.image = uploadService.files[0].url;
                new ContactUs($scope.contactUs).$save().then(function() {
                    $scope.showContactUs();
                });
            };

            $scope.noImage = function() {
                if (uploadService.files.length < 1) {
                    return true;
                } else {
                    return false;
                }
            };
    }]);




